#include "ejconfigmanager.h"
EJConfigManager *EJConfigManager::m_instance = new EJConfigManager;


EJConfigManager *EJConfigManager::getInstance()
{
    return m_instance;
}

bool EJConfigManager::addConfig(const QString &name)
{
    bool ret = false;
    do
    {
        auto cnf = new EJConfig;
        if (!cnf->open(name)) break;
        m_map.insert(name, cnf);
        ret = true;
    }while(0);
    return ret;
}

EJConfig *EJConfigManager::delConfig(const QString &name)
{
    EJConfig *ret = nullptr;
    auto cnf = m_map.find(name);

    do
    {
        if (cnf == m_map.end())break;

        ret = cnf.value();

        m_map.erase(cnf);

    }while(0);

    return ret;
}

EJConfig *EJConfigManager::getConfig(const QString &name)
{
    EJConfig *ret = nullptr;
    auto cnf = m_map.find(name);

    do
    {
        if (cnf == m_map.end())break;

        ret = cnf.value();

    }while(0);

    return ret;

}

bool EJConfigManager::putConfig(const QString &name, EJConfig *cnf)
{
    if (cnf == nullptr || name.length() == 0) return false;

    m_map.insert(name, cnf);

    return true;
}

QString EJConfigManager::getAttrByName(const QString &xmlName, const QStringList &lable, const QString &attrName)
{
    QString ret = "";
    auto cnf = m_instance->getConfig(xmlName);

    do
    {
        if (cnf == nullptr) break;

        auto attr = cnf->getAttr(lable);

        if (attr.empty()) break;

        auto it = attr.find(attrName);

        if (it == attr.end()) break;

        ret = it.value();

    }while(0);

    return ret;
}

QStringList EJConfigManager::getAttr(const QString &xmlName, const QStringList &lable, const QString &subLable, const QString &attrName)
{
    QStringList ret;
    auto cnf = m_instance->getConfig(xmlName);
    do
    {
        if (cnf == nullptr) break;

        ret = cnf->getAttr(lable, subLable, attrName);

    }while(0);

    return ret;
}

QStringList EJConfigManager::getValueByName(const QString &xmlName, const QStringList &lable, const QString &subLable)
{
    QStringList ret;
    auto cnf = m_instance->getConfig(xmlName);
    do
    {
        if (cnf == nullptr) break;

        ret = cnf->getValue(lable, subLable);

    }while(0);

    return ret;
}
